/*
Copyright (C) 2010 
Author		  Oliver Schenk
Contact		  Oliver.Schenk76@gmail.com

This program is free software; you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; 
if not, see <http://www.gnu.org/licenses/>.
*/

<%@ page  language="java" import="java.util.*,java.text.*"%>
<%@ page import="java.net.MalformedURLException" %>
<%@ page import="java.net.URL" %>
<%@ page import="com.google.gdata.client.calendar.*" %>
<%@ page import="com.google.gdata.data.calendar.*" %>
<%@ page import="com.google.gdata.client.*" %>
<%@ page import="com.google.gdata.data.*" %>
<%@ page import="com.google.gdata.model.*" %>
<%@ page import="com.google.gdata.util.*" %>
<%@ page import="com.google.gdata.data.calendar.*" %>
<%@ page import="com.google.gdata.data.extensions.*" %>
<%@ page import="com.google.gdata.data.*" %>
<%@ page import="com.google.gdata.client.http.*" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="SelfBookingSystem.AdminValues" %>
<%@ page import="SelfBookingSystem.PMF" %>
<%@ page import="javax.servlet.http.HttpServletResponse"%>

<%!
public int nullIntconv(String inv)
{   
	int conv=0;
		
	try{
		conv=Integer.parseInt(inv);
	}
	catch(Exception e)
	{}
	return conv;
}

%>
<%
//Variablen and Format declaration
Boolean belegt = new Boolean(false);
SimpleDateFormat DayFormat = new SimpleDateFormat("dd.MM.");
SimpleDateFormat MonthFormat = new SimpleDateFormat("MM");
SimpleDateFormat YearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat ArrayFormat = new SimpleDateFormat("yyyy-MM-dd");
String GoogleCalendarURL = new String();
String EmailAddress = new String();
String Password = new String();
String AppID = new String();
String BodyBackgroundColor = new String();

if(request.getParameter("AppID") != null){
	AppID = request.getParameter("AppID");
}else{
	//HttpServletResponse resp;
	//resp.sendRedirect("/Error.jsp");
	AppID = "xxx";
}

Map<String, String> m = Selbstbucher.GetConfigurationValues.GetConfigurationValues(AppID);
GoogleCalendarURL = m.get("GoogleCalendarURL");
EmailAddress = m.get("EmailAddress");
Password = m.get("Password");
BodyBackgroundColor = m.get("BodyBackgroundColor");
    
if(GoogleCalendarURL == null){
	response.sendRedirect("/Error.jsp");	
}
    
int WeekOfYear = nullIntconv(request.getParameter("WeekOfYear"));


 Calendar ca = new GregorianCalendar();
 int iTDay=ca.get(Calendar.DATE);
 int iTYear=ca.get(Calendar.YEAR);
 int iTMonth=ca.get(Calendar.MONTH)+1;

	String today = iTDay + "." + iTMonth + "." + iTYear;

	if (WeekOfYear==0){
		WeekOfYear = ca.get(Calendar.WEEK_OF_YEAR);
	}
	
	int PreviousWeek = WeekOfYear - 1;
	int NextWeek = WeekOfYear + 1;
	ca.set(Calendar.WEEK_OF_YEAR, WeekOfYear);
    
    
    
    String month = MonthFormat.format(ca.getTime());
    String year = YearFormat.format(ca.getTime());
    ArrayList DayOfWeek = new ArrayList();
    
    ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);	
	String monday = DayFormat.format(ca.getTime());
	DayOfWeek.add(ArrayFormat.format(ca.getTime()));
    ca.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);	
	String tuesday = DayFormat.format(ca.getTime());
    DayOfWeek.add(ArrayFormat.format(ca.getTime()));
    ca.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);	
	String wednesday = DayFormat.format(ca.getTime());
	DayOfWeek.add(ArrayFormat.format(ca.getTime()));
    ca.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);	
	String thursday = DayFormat.format(ca.getTime());
	DayOfWeek.add(ArrayFormat.format(ca.getTime()));
    ca.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);	
	String friday = DayFormat.format(ca.getTime());
	DayOfWeek.add(ArrayFormat.format(ca.getTime()));
    ca.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);	
	String saturday = DayFormat.format(ca.getTime());
	DayOfWeek.add(ArrayFormat.format(ca.getTime()));
    ca.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
	String sunday = DayFormat.format(ca.getTime());
	DayOfWeek.add(ArrayFormat.format(ca.getTime()));	
	
	
	ArrayList OpeningHours = new ArrayList(); 
	OpeningHours.add("08:00"); 
	OpeningHours.add("09:00");
	OpeningHours.add("10:00");
	OpeningHours.add("11:00");
	OpeningHours.add("12:00");
	OpeningHours.add("13:00");
	OpeningHours.add("14:00");
	OpeningHours.add("15:00");
	OpeningHours.add("16:00");
	
		URL feedUrl = new URL(GoogleCalendarURL);
		CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
		myService.setUserCredentials(EmailAddress, Password);		
		// Send the request and receive the response:
		CalendarEventFeed myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
	
	
	ArrayList Events = new ArrayList();
	for (int i = 0; i < myFeed.getEntries().size(); i++) {
		CalendarEventEntry entry = myFeed.getEntries().get(i);
        List timeList = entry.getTimes(); 
		for (int j=0; j<timeList.size();j++){
			When eventTime = (When) timeList.get(j); 
			DateTime startTime = eventTime.getStartTime(); 
			DateTime endTime = eventTime.getEndTime(); 
			String startTimeStr = startTime.toUiString(); 
			String endTimeStr = endTime.toUiString(); 
			Events.add(startTimeStr);
		}				  
	}
	
%>
<html>
<head>
<title>How to create Calendar in JSP</title>
</head>
<body bgcolor="<%=BodyBackgroundColor%>">
<a href="Calendar.jsp?AppID=<%=AppID%>&WeekOfYear=<%=PreviousWeek%>">Previous</a> | <a href="Calendar.jsp?AppID=<%=AppID%>&WeekOfYear=<%=NextWeek%>">Next</a> 
<table border=1>
	<tr>
		<th colspan=8>
		<%=year%>
		</th>
	</tr>
	<tr><th></th>
		<th><%=monday%></th>
		<th><%=tuesday%></th>
		<th><%=wednesday%></th>
		<th><%=thursday%></th>
		<th><%=friday%></th>
		<th><%=saturday%></th>
		<th><%=sunday%></th>
	</tr>
	<%
		for(int k=0; k<OpeningHours.size(); k++){
		%>
			<tr>
				<td>
				<%=OpeningHours.get(k)%>
				</td>
				<%
				for(int i=0; i<DayOfWeek.size(); i++){
				
					for(int j=0;j<Events.size();j++){
						String tmp = Events.get(j).toString();
						String tmp1 = OpeningHours.get(k).toString();
						String tmp2 = DayOfWeek.get(i).toString();
					
				//		System.out.println("Event:" + tmp);
				//		System.out.println("OpeningHours:" + tmp1);
				//		System.out.println("DayOfWeek:" + tmp2);
						
						belegt = new Boolean(false);
						if(tmp.contains(tmp1) && tmp.contains(tmp2)){
							belegt = new Boolean(true);
							break;
						}
					}
					
				
				
					if(belegt){
					%>	
					<td>
						Belegt
					</td>
					<%	
					}else{
					%>
					<td>
						<a href="BookAppointment.jsp?AppID=<%=AppID%>&Time=<%=OpeningHours.get(k)%>&Date=<%=DayOfWeek.get(i)%>">Termin buchen</a>
					</td>
					<%
					}
				}	
				%>
				
			</tr>
		
		<%
		}
	
	
	%>
</table>

</body>
</html>
